//****************************************************************************
//Copyright (C) 2005 Adobe Systems, Inc. All Rights Reserved.
//The following is Sample Code and is subject to all restrictions on
//such code as contained in the End User License Agreement accompanying
//this product.
//****************************************************************************

class KeyListener {
	
	//---------------------------------------------- Properties
	
	private var curMenu:Number;					// store the currently choosen menuItem
	private var state:String;						// store the state of the app, can be: menu, image, video, or audio
	
	//---------------------------------------------- Methods
	
	/**
	 * constructor function
	 * 
	 */
  	public function KeyListener() {
  		setSelectedMenu(1);						// set curMenu to 1 by default
  		setSoftKeys(null, "Quit");				// set the initial soft key labels	
  		setState("menu");						// set initial app state
  	} 	
  	
  	/**
  	 * onKeyDown method fired when a key is pressed
  	 * 
  	 */
  	public function onKeyDown() {
  		var code = Key.getCode();
  		var curState = getState();
		
  		switch(code) {
  			case ExtendedKey.SOFT1:				// left soft key
  				switch(getState()) {
					case "menu":
						// do nothing
						break;
					case	 "image":
						// go back
						goBack();
						break;
					case "video":
						// go back
						goBack();
						break;
					case "audio":
						// go back
						goBack();
						break;
				}
  				break;
  			case ExtendedKey.SOFT2:				// right soft key
  				switch(getState()) {
					case "menu":
						// quit
						fscommand2("quit");
						break;
					case	 "image":
						// quit
						fscommand2("quit");
						break;
					case "video":
						// pause/play
						mediaEvent();
						break;
					case "audio":
						// pause/play
						mediaEvent();
						break;
				}
  				break;
  			case 40:								// down
  				// change the highlight
  				var temp = getSelectedMenu();
  				if (temp < 5)
  					setSelectedMenu(temp+1);
  				break;
  			case 38:								// up
  				// change the highlight
  				var temp = getSelectedMenu();
  				if (temp > 1)
  					setSelectedMenu(temp-1);
  				break;
  			case 13:								// enter
  				// broadcast event
  				var temp = getSelectedMenu();
  				event(temp);
  				temp = null;
  				delete temp;
				break;
  			case 49:								// 1
  				event(1);
  				break;
  			case 50:								// 2
  				event(2);
  				break;
  			case 51:								// 3
  				event(3);
  				break;
  			case 52:								// 4
  				event(4);
  				break;
  			case 53: 							// 5
  				event(5);
  				break;
  		}
  	}
  	
  	/**
  	 * event method
  	 * 
  	 * @param menu	used to pass menu selected
  	 */
  	 private function event(menu:Number):Void {
  	 	if (getState() == "menu") {
  	 		var temp = "menu" + menu;
  	 		_root[temp].eventCall();
			
			// turn off bottom message
			_root.bottomText_mc.bottom_txt.text = "";
			
			// set softkeys and state
			switch (_root[temp].getClassType()) {
				case "ImageViewer":
					setSoftKeys("Back","Quit");
					setState("image");
					break;
				case "VideoViewer":
					setSoftKeys("Back","Pause");
					setState("video");
					break;
				case "SoundPlayer":
					setSoftKeys("Back", "Stop");
					setState("audio");
					break;
			}
				
			// disable all the menu items
			controlItems(0);
			
			// clean up
			temp = null;
			delete temp;
  	 	}
  	 } 
  	 
  	/**
  	 * goBack method
  	 */
  	 private function goBack() {
  	 	setState("menu");
		controlItems(1);
		// destory current viewer
		var name = "menu" + getSelectedMenu();
		_root[name].destroyViewer();
		setSoftKeys(null, "Quit");
		_root.bottomText_mc.bottom_txt.text = "use your phone's number keys" + newline + "to select an entry";
		// clean up
		name = null;
		delete name;
  	 }
  	 
  	/**
  	 * getSelectedMenu
  	 * 
  	 */
  	 private function getSelectedMenu():Number {
  	 	// use current var (curMenu)
  	 	return curMenu;
  	 }
  	  
  	  
  	/**
  	 * setSelectedMenu
  	 * 
  	 * @param choosen	string representing up or down from current menu
  	 */
  	 private function setSelectedMenu(choosen:Number):Void {
  	 	// turn off current selection
  	 	var name = "menu" + getSelectedMenu();
  	 	_root[name].setHighlight(false);
  	 	
  	 	// turn on new selection
  	 	var temp = "menu" + choosen;
  	 	_root[temp].setHighlight(true);
  	 	
  	 	// set the curMenu var
  	 	curMenu = choosen;
  	 	
  	 	// clean up
  	 	name = null;
  	 	delete name;
  	 	temp = null;
  	 	delete temp;
  	 }
  	 
  	/**
  	 * setSoftKeys
  	 * 
  	 * @param left		label for left soft key
  	 * @param right		label for right soft key
  	 */
  	 private function setSoftKeys(left:String, right:String):Void {
  	 	if (left != undefined || left != null)
  	 		_root.left_mc.leftFore_txt.text = _root.left_mc.leftBack_txt.text = left;
  	 	if (left == null)
  	 		_root.left_mc.leftFore_txt.text = _root.left_mc.leftBack_txt.text = "";
  	 	if (right != undefined || right != null)
  	 		_root.right_mc.rightFore_txt.text = _root.right_mc.rightBack_txt.text = right;
  	 	fscommand2("SetSoftKeys", left, right);
  	 }
  	 
  	/**
  	 * getState
  	 */
  	 private function getState():String {
  	 	return state;
  	 }
  	 
  	/**
  	 * setState
  	 * 
  	 * @param nextState		used to store the new state of the application
  	 */
  	 private function setState(nextState:String):Void {
  	 	state = nextState;
  	 }
  	 
  	/**
  	 * controlItems
  	 * 
  	 * @param state	 state of visibility for menu items
  	 */
  	 private function controlItems(state:Number):Void {
  	 	_root.choose_mc._visible = state;
		var i=0;
		while (i<=5) {
			var item = "menu" + i;
			_root[item].enableClip(state);
			i++;
		}
		i=null;
		delete i;
		item=null;
		delete item;
	}
	
	/**
  	 * mediaEvent method
  	 * 
  	 */
  	 private function mediaEvent():Void {
  	 	if (getState() == "video") {
  	 		var temp = "menu" + getSelectedMenu();
  	 		var state = _root.viewer.getState();
  	 		if (state == true) {
  	 			_root.viewer.videoPause();
  	 			_root.viewer.setState(false);
  	 			setSoftKeys("Back", "Play");
  	 		} else {
  	 			_root.viewer.videoResume();
  	 			_root.viewer.setState(true);
  	 			setSoftKeys("Back", "Pause");
  	 		}
  	 		temp = null;
  	 		delete temp;
  	 		state = null;
  	 		delete state;
  	 	} else if (getState() == "audio") {
  	 		_root.viewer.getState();
  	 		var temp = "menu" + getSelectedMenu();
  	 		var state = _root.viewer.getState();
  	 		if (state == true || state == undefined) {
  	 			_root.viewer.soundStop();
  	 			_root.viewer.setState(false);
  	 			setSoftKeys("Back", "Play");
  	 		} else {
  	 			_root.viewer.soundPlay();
  	 			_root.viewer.setState(true);
  	 			setSoftKeys("Back", "Stop");
  	 		}
  	 		temp = null;
  	 		delete temp;
  	 		state = null;
  	 		delete state;
  	 	}
  	 }
  	 
}